package com.starbaba.channelmanager.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.starbaba.channelmanager.model.ActivityChannelModel;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.model.dictionary.Constant;

@SuppressWarnings({"deprecation", "rawtypes"})
@Component
public class ActivityChannelService {

	private static BaseDao dao = new BaseDao(Constant.POOL_LIFE);
	
	public void showPage(Map<String, Object> paramsMap) throws Exception{
		List<ActivityChannelModel> channelList = dao
				.list("select channel, channel_name from activity_channel_config a, comm_product_channel b where a.channel = b.channel_code", ActivityChannelModel.class);
		paramsMap.put("channelList", channelList);
	}
	
	public List<ActivityChannelModel> getList(Integer start, Integer length, PageResponse pageResponse, Map<String, Object> paramsMap) throws Exception {
		
		String channel = (String) paramsMap.get("channel");
		String remark = (String) paramsMap.get("remark");
		
		StringBuilder whereParams = new StringBuilder();
		if (StringUtils.isNotEmpty(channel)) {
			whereParams.append("and channel = '" + channel + "' ");
		}
		if (StringUtils.isNotEmpty(remark)) {
			whereParams.append("and remark like '%" + remark + "%' ");
		}
		
		StringBuilder sql_list = new StringBuilder("select * from activity_channel_config where 1 = 1 ");
		sql_list.append(whereParams).append("order by ctime desc ");
		
		String sqlCount = "select count(1) from (" + sql_list.toString() + ") b ";
		if (start != null && length != null) {
			sql_list.append("limit " + start + ", " + length);
		}
		
		int count = dao.getInt(sqlCount);
		List<ActivityChannelModel> list = dao.list(sql_list.toString(), ActivityChannelModel.class);
		
		pageResponse.setITotalRecords(count);
		pageResponse.setITotalDisplayRecords(count);
		
		return list;
	}

	public String deleteInfo(Map<String, Object> paramsMap) throws Exception {
		String message = "";
		String id = (String) paramsMap.get("id");
		if (StringUtils.isEmpty(id)) {
			message = "id 不存在 ！！！";
		} else {
			int count = dao.delete("delete from activity_channel_config where id = ? ", new Object[]{id});
			message = "成功删除" + count + "条记录！！！";
		}
		return message;
	}
	
	public void getInfo(Map<String, Object> paramsMap) throws Exception {
		
		List<ActivityChannelModel> channelList = dao.list("select channel_code channel, channel_name from comm_product_channel", ActivityChannelModel.class);
		paramsMap.put("channelList", channelList);
		
		String id = (String) paramsMap.get("id");
		ActivityChannelModel mode = dao.get("select * from activity_channel_config where id = ? ", ActivityChannelModel.class, id);
		if (mode == null) {
			paramsMap.put("model", new ActivityChannelModel());
		} else {
			paramsMap.put("model", mode);
		}
	}
	
	public String editInfo(Map<String, Object> paramsMap) throws Exception {
		
		String id = (String) paramsMap.get("id");
		String channel = (String) paramsMap.get("channel");
		String activity = (String) paramsMap.get("activity");
		String remark = (String) paramsMap.get("remark");
		String operator = (String) paramsMap.get("operator");
		String url = "http://tuanzidai.cn/frontend_loan_service/common?appid=3&funid=117&isapp=1&activity=" + activity + "&channel=" + channel;
		
		String message = "";
		ActivityChannelModel mode = new ActivityChannelModel(channel, activity, remark, operator, url);
		
		if (StringUtils.isEmpty(id)) {
			// 新增
			dao.add(mode);
			message = "新增成功！！！";
		} else {
			// 修改
			mode.setId(Long.valueOf(id));
			int i = dao.update(mode, "id");
			message = "成功更新" + i + "条记录！！！";
		}
		return message;
	}
	
}
